package com.yhxd.modular.news.controller;

import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.yhxd.modular.news.entity.Classify;
import com.yhxd.modular.news.entity.News;
import com.yhxd.modular.news.entity.QNews;
import com.yhxd.modular.news.service.AllInformationService;
import com.yhxd.modular.news.service.AttachmentService;
import com.yhxd.modular.news.service.ClassifyService;
import com.yhxd.modular.system.entity.Resource;
import com.yhxd.modular.system.vo.ResultVo;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = "/all/info")
@Api
public class AllInformationController {
    @Autowired
    private AllInformationService allInformationService;

    @Autowired
    private ClassifyService classifyService;

    @Autowired
    private AttachmentService attachmentService;

    /**
     * 跳转到列表页
     * @return
     */
    @GetMapping(value = "/list")
    @RequiresPermissions("all_info_list")
    public String list() {
        return "mng/allinfo/allinfo";
    }

    /**
     * 跳转到详情
     * @return
     */
    @GetMapping(value = "/toDetail/{id}")
    @RequiresPermissions("allInfo_detail")
    public String toDetail(Model model, @PathVariable("id") Long id) {
        News news = this.allInformationService.findById(id);
        model.addAttribute("news", news);
        List<Classify> classify = classifyService.findClassify(null, 0);
        List<Classify> childClassify = classifyService.findClassify(news.getParentClassify().getId(), 1);
        model.addAttribute("classify", classify);
        model.addAttribute("childClassify", childClassify);
        return "mng/allinfo/allInfo_detail";
    }

    /**
     * 查询列表数据
     * @param page
     * @param limit
     * @param dto
     * @return
     */
    @PostMapping(value = "/find_info_list")
    @RequiresPermissions("all_info_list")
    @ResponseBody
    public ResultVo findList(Integer page, Integer limit,News dto) {
        try {
            QPageRequest pageRequest = QPageRequest.of(page - 1, limit, new OrderSpecifier(Order.DESC, QNews.news.createTime));
            Page<News> resourcePage = this.allInformationService.findAll(pageRequest, dto);
            return ResultVo.success(resourcePage);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }

    /**
     * 删除
     * @return
     */
    @GetMapping(value = "/del/{id}")
    @RequiresPermissions("allInfo_del")
    @ResponseBody
    public ResultVo del(@PathVariable("id") Long id) {
        try {
            this.allInformationService.deleteById(id);
            return ResultVo.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVo.error(e.getMessage());
        }
    }



}
